<?php
namespace Admin\Model;

use Think\Model;
use Think\Page;

class TestKeyOrderModel extends Model{
    public function index(){
    
    }
    public function Order_List(){
        $page=$this->get_page();
        $result['show']=$page->show();
        $result['list']=$this->field('bls_testkey_order.name,order_id,title,bls_testkey_order.price,bls_testkey_order.addtime,bls_testkey_order.phone,bls_testkey_order.cardNo')->join('bls_user  ON bls_testkey_order.user_id=bls_user.id')->join('bls_testkey ON bls_testkey.id=bls_testkey_order.key_id ')->limit($page->firstRow.','.$page->listRows)->select();
        return   $result;
    }
    
    public function Count_money(){
        return $this->sum('price');
    }
    
    public function Count_money_bySearch($map){
        return $this->where($map)->sum('price');
    }
    
    public function add_order($data){
        $re=$this->add($data)? true:false;
        return $re;
    }
    
    public function delete_order($info){
         
        $map['order_id']=$info['order_id'];
        $money=$this->where($map)->sum('price');
        $result=$this->where($map)->delete();
        $data['delCount']=$result;
        $data['money']=$money;
        return $data;
    
    }
     
    public function delete_all($info){
        $re= array();
        $re=explode('@',$info['ids']);
        if(!empty($re)){
            $map['order_id']=array('in',$re);
            $money=$this->where($map)->sum('price');
            $result=$this->where($map)->delete();
            $data['delCount']=$result;
            $data['money']=$money;
            return $data;
        }
        else{
            return false;
        }
         
    }
     
    public function query_order($data){
        $current_day=date('Y-m-d',time());
        $default_day=strtotime($current_day." 23:59:59");//默认的查询的截止日期为当天23:59:59
        if($data['starttime']==''&&$data['endtime']==''){
            $data['starttime']=strtotime('1970-01-01');
            $data['endtime']= $default_day;
        }else if($data['starttime']!=''&&$data['endtime']!=''){
            $data['starttime']=strtotime($data['starttime']."00:00:00");
            $data['endtime']=strtotime($data['endtime']."23:59:59");
        }else{
            $temp_time=($data['starttime']==''?$data['endtime']:$data['starttime']);
            $data['starttime']=strtotime($temp_time."00:00:00");
            $data['endtime']=strtotime($temp_time."23:59:59");
        }
        //         $ID=M('user')->where(['nickname'=>$data['nickname']])->find();
        $map['bls_testkey_order.name']=array('like','%'.$data['name'].'%');
        $map['bls_testkey_order.addtime']=array(array('EGT',$data['starttime']),array('ELT',$data['endtime']),'AND');
        $map['order_id']=array('like','%'.$data['order_id'].'%');
        $map['_logic']='and';
        $page=$this->get_page($map);
        $result['show']= $page->show();
        $result['list']=$this->field('bls_testkey_order.name,order_id,title,bls_testkey_order.price,bls_testkey_order.addtime,bls_testkey_order.phone,bls_testkey_order.cardNo')->join('bls_user  ON bls_testkey_order.user_id=bls_user.id')->join('bls_testkey ON bls_testkey.id=bls_testkey_order.key_id ')->where($map)->limit($page->firstRow.','.$page->listRows)->select();
        $result['money']=$this->Count_money_bySearch($map);
        return $result;
    }
    
    public function get_page($map){
         
        $count      = $this->where($map)->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(1)
        $Page -> setConfig('prev','上一页');
        $Page -> setConfig('next','下一页');
        $Page->setConfig('theme', ' %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER% ');
        return $Page;
         
    }
}